package net.kesumu.yushabuserver.daoimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import net.kesumu.yushabuserver.dao.IFriendRelationshipDao;
import net.kesumu.yushabuserver.entity.FriendRelationship;
import net.kesumu.yushabuserver.utils.DataSource;

public class FriendRelationshipDaoImpl implements IFriendRelationshipDao{

    public FriendRelationshipDaoImpl() {
    }

    @Override
    public ArrayList<FriendRelationship> getFriendRelationshipByUserId(
            int userId) {
        ArrayList<FriendRelationship> friendRelationships = new ArrayList<FriendRelationship>();
        DataSource ds = new DataSource();
        Connection conn = ds.getConnection();
        String queryStatement_FriendRelationship = 
                "SELECT * FROM " + DataSource.dbName + ".friendrelationship "
                + "WHERE userId = '" + userId + "'";
        ResultSet resultSet = null;
        try {
            resultSet = conn.prepareStatement(queryStatement_FriendRelationship).executeQuery();
            while (resultSet.next()) {
                FriendRelationship friendRelationship = new FriendRelationship();
                friendRelationship.setFriendRelationshipId(resultSet.getInt(1));
                friendRelationship.setFriendUserId(resultSet.getInt(2));
                friendRelationship.setLevel(resultSet.getInt(3));
                friendRelationship.setUserId(resultSet.getInt(4));
                friendRelationships.add(friendRelationship);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            ds.close();
        }
        return friendRelationships;
    }
    
    

}
